package com.self.av;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class ActionLineView extends View {
    private Paint linePaint;
    private List<PointF[]> lines = new ArrayList<>();
    private Paint circlePaint; // 新增画圆的画笔
    private List<PointF> circles = new ArrayList<>(); // 新增存储圆中心位置的列表
    private Paint pathPaint;
    private List<PointF> pathPointsLeftElbow = new ArrayList<>();
    private List<PointF> pathPointsRightElbow = new ArrayList<>();
    private List<PointF> pathPointsLeftWrist = new ArrayList<>();
    private List<PointF> pathPointsRightWrist = new ArrayList<>();
    private PointF headPoint = null;
    private PointF leftEyePoint = null;
    private PointF rightEyePoint = null;
    private PointF leftShoulderPoint = null;
    private PointF rightShoulderPoint = null;
    private boolean showAction = true;
    private boolean showPath = false;
    // 新增眼睛线段画笔
    private Paint eyeLinePaint;
//    // 新增鼻子垂直线画笔
//    private Paint noseVerticalLinePaint;

    public ActionLineView(Context context) {
        super(context);
        init();
    }

    public ActionLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ActionLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 修改线条颜色为浅蓝灰色
        linePaint = new Paint();
        linePaint.setColor(Color.rgb(200, 220, 240));
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(5);

        // 修改圆圈颜色为浅粉色
        circlePaint = new Paint(); // 初始化画圆的画笔
        circlePaint.setColor(Color.rgb(250, 220, 220));
        circlePaint.setStyle(Paint.Style.STROKE); // 设置填充样式
        circlePaint.setStrokeWidth(5); // 设置笔触宽度

        // 修改路径点颜色为浅绿灰色
        pathPaint = new Paint();
        pathPaint.setColor(Color.rgb(220, 240, 220));
        pathPaint.setStyle(Paint.Style.FILL);
        pathPaint.setStrokeWidth(10);

        // 初始化眼睛线段画笔，设置颜色为红色
        eyeLinePaint = new Paint();
        eyeLinePaint.setColor(Color.RED);
        eyeLinePaint.setStyle(Paint.Style.STROKE);
        eyeLinePaint.setStrokeWidth(5);

//        // 初始化鼻子垂直线画笔，设置颜色为蓝色
//        noseVerticalLinePaint = new Paint();
//        noseVerticalLinePaint.setColor(Color.BLUE);
//        noseVerticalLinePaint.setStyle(Paint.Style.STROKE);
//        noseVerticalLinePaint.setStrokeWidth(5);
    }

    public void clear(){
        this.lines.clear();
        this.circles.clear();
        invalidate();
    }
    public void setLines(List<PointF[]> newLines) {
        this.lines = newLines;
//        invalidate(); // 触发重绘
    }

    public void setCircles(List<PointF> newCircles) { // 新增设置圆数据的方法
        this.circles = newCircles;
//        invalidate(); // 触发重绘
    }

    public void addPathPoints(List<PointF> points) { // 新增设置圆数据的方法
        if (showPath) {
            pathPointsLeftElbow.add(points.get(0));
            pathPointsRightElbow.add(points.get(1));
            pathPointsLeftWrist.add(points.get(2));
            pathPointsRightWrist.add(points.get(3));
//            if (headPoint != null) {
//                this.headPathPoints.add(headPoint);
//            }
        }
    }

    public void clearPathPoints() { // 新增设置圆数据的方法
        showPath = false;
        pathPointsLeftElbow.clear();
        pathPointsRightElbow.clear();
        pathPointsLeftWrist.clear();
        pathPointsRightWrist.clear();
//        this.headPathPoints.clear();
    }

    public void setShowPath(boolean status) { // 新增设置圆数据的方法
        showPath = status;
    }

    public void setShowAction(boolean status) { // 新增设置圆数据的方法
        showAction = status;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制线条
        for (PointF[] line : lines) {
            if (line.length == 2 && showAction) {
                canvas.drawLine(line[0].x, line[0].y, line[1].x, line[1].y, linePaint);
            }
        }
        // 绘制圆圈
        for (PointF point : circles) {
            if (point != null && showAction) {
                canvas.drawCircle(point.x, point.y, 10, circlePaint); // 绘制圆
            }
        }

        // 绘制四个关节的路径线
        drawPathLines(canvas, pathPointsLeftElbow);
        drawPathLines(canvas, pathPointsRightElbow);
        drawPathLines(canvas, pathPointsLeftWrist);
        drawPathLines(canvas, pathPointsRightWrist);

//        for (PointF point : pathPoints) {
//            if (point!= null) {
//                canvas.drawCircle(point.x, point.y, 10, pathPaint); // 绘制圆
//            }
//        }

//        for (PointF point : headPathPoints) {
//            if (point!= null) {
//                canvas.drawCircle(point.x, point.y, 30, pathPaint); // 绘制圆
//            }
//        }

        if (leftEyePoint != null && rightEyePoint != null && headPoint != null && showAction && leftShoulderPoint != null && rightShoulderPoint != null) {
            PointF point0 = leftShoulderPoint;
            PointF point1 = rightShoulderPoint;
            // 使用勾股定理计算两点间距离
            // 计算目标长度
            float targetLength = (float) Math.sqrt(Math.pow(point1.x - point0.x, 2) + Math.pow(point1.y - point0.y, 2));
            int radius = (int) (targetLength / 4);
            canvas.drawCircle(headPoint.x, headPoint.y, radius, circlePaint);
            // 计算原始线段长度
            float originalLength = (float) Math.sqrt(Math.pow(rightEyePoint.x - leftEyePoint.x, 2) + Math.pow(rightEyePoint.y - leftEyePoint.y, 2));
            // 计算延长比例
            float scaleFactor = targetLength / originalLength;
            // 计算延长后的端点坐标
            float dx = rightEyePoint.x - leftEyePoint.x;
            float dy = rightEyePoint.y - leftEyePoint.y;
            float newLeftX = leftEyePoint.x - dx * (scaleFactor - 1) / 2;
            float newLeftY = leftEyePoint.y - dy * (scaleFactor - 1) / 2;
            float newRightX = rightEyePoint.x + dx * (scaleFactor - 1) / 2;
            float newRightY = rightEyePoint.y + dy * (scaleFactor - 1) / 2;
            // 绘制延长后的线段
            canvas.drawLine(newLeftX, newLeftY, newRightX, newRightY, eyeLinePaint);

//            // 计算垂直线段的长度
////            float lineLength = (float) Math.sqrt(Math.pow(newRightX - newLeftX, 2) + Math.pow(newRightY - newLeftY, 2));
//            float halfLength = targetLength / 2;
//
//            // 计算垂直向量
//            float perpendicularDx = -(newRightY - newLeftY);
//            float perpendicularDy = newRightX - newLeftX;
//
//            // 归一化垂直向量
//            float perpendicularLength = (float) Math.sqrt(perpendicularDx * perpendicularDx + perpendicularDy * perpendicularDy);
//            perpendicularDx /= perpendicularLength;
//            perpendicularDy /= perpendicularLength;
//
//            // 计算垂直线段的端点
//            float verticalStartX = headPoint.x - perpendicularDx * halfLength;
//            float verticalStartY = headPoint.y - perpendicularDy * halfLength;
//            float verticalEndX = headPoint.x + perpendicularDx * halfLength;
//            float verticalEndY = headPoint.y + perpendicularDy * halfLength;
//
//            // 绘制垂直线段
//            canvas.drawLine(verticalStartX, verticalStartY, verticalEndX, verticalEndY, noseVerticalLinePaint);
        }

    }

    private void drawPathLines(Canvas canvas, List<PointF> pathPoints) {
        if (showPath && pathPoints.size() > 1) {
            for (int i = 0; i < pathPoints.size() - 1; i++) {
                PointF currentPoint = pathPoints.get(i);
                PointF nextPoint = pathPoints.get(i + 1);
                if (currentPoint != null && nextPoint != null) {
                    canvas.drawLine(currentPoint.x, currentPoint.y, nextPoint.x, nextPoint.y, pathPaint);
                }
            }
        }
    }

    public void setHeadPoint(PointF jointPosition) {
        this.headPoint = jointPosition;
    }

    public void setEyePoints(PointF leftEye, PointF rightEye) {
        this.leftEyePoint = leftEye;
        this.rightEyePoint = rightEye;
    }

    public void setShoulders(PointF leftShoulder, PointF rightShoulder) {
        this.leftShoulderPoint = leftShoulder;
        this.rightShoulderPoint = rightShoulder;
    }

}
